<template>
  <div>
    <el-row>
      <el-button type="primary" style="margin-bottom: 12px" @click="handleAdd"
        >添加</el-button
      >
      <el-table
        :data="tableData"
        class="table-body"
        :header-cell-style="{ background: '#F4F6F9', color: '#1D2129' }"
      >
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="roleName" label="名称"></el-table-column>
        <el-table-column prop="roleCode" label="角色代码"></el-table-column>
        <el-table-column
          prop="updateDate"
          show-overflow-tooltip
          width="220"
          label="更新时间"
        ></el-table-column>
        <el-table-column label="操作" width="160" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleConfigPermission(scope.row.roleId)"
              >菜单配置</el-button
            >
            <el-button type="text" @click="handleUpdate(scope.row.roleId)"
              >编辑</el-button
            >
            <!--            <el-button type="text" @click="handleStatus(scope.row)">{{ scope.row.isDisable ? '禁用' : '启用' }}</el-button>-->
            <el-button type="text" @click="handleDelete(scope.row.roleId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :count="Number(count)"
        :current="1"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
    </el-row>
    <Add ref="add" @getParentTableData="getParentTableData" />
    <Detail ref="detail" @getParentTableData="getParentTableData" />
    <Permission
      ref="permission"
      :tableData="menuList"
      @getParentTableData="getParentTableData"
    />
  </div>
</template>

<script>
import Pagination from "@/components/Pagination.vue";
import Add from "./Add";
import Permission from "./Permission";
import Detail from "./Detail";
import { deleteRoleApi } from "@/api/modules/role.api";
import { getMenuListApi } from "@/api/modules/menu.api";

export default {
  components: { Add, Pagination, Permission, Detail },
  props: ["tableData", "count"],
  data() {
    return {
      status: false,
      page: {
        pageNum: 1,
        pageSize: 10,
      },
      menuList: [],
    };
  },
  methods: {
    handleSizeChange(pageSize) {
      this.page.pageNum = pageSize;
      this.$emit("getTableData", this.page);
    },
    handleCurrentChange(pageNum) {
      this.page.pageNum = pageNum;
      this.$emit("getTableData", this.page);
    },
    async handleConfigPermission(id) {
      this.menuList = await getMenuListApi();
      await this.$refs.permission.init(id);
    },
    getParentTableData() {
      this.$emit("getTableData", this.page);
    },
    handleAdd() {
      this.$refs.add.init();
    },
    handleUpdate(id) {
      this.$refs.detail.init(id);
    },
    async handleDelete(id) {
      await deleteRoleApi(id);
      this.$message({
        message: "成功",
        type: "success",
      });
      this.$emit("getTableData", this.page);
    },
  },
};
</script>
